﻿using System;
using System.Reflection;
using FluentNHibernate.Conventions;

namespace NMA.Infrastructure.NHibernate.Mapping.Convention
{
    public class CustomForeignKeyConvention : ForeignKeyConvention
    {
        protected override string GetKeyName(PropertyInfo property, Type type)
        {
            if (property == null)
                return type.Name + "ID";

            string fullName = property.PropertyType.FullName;
            int lastIndexOf = fullName.LastIndexOf(".") + 1;
            int length = fullName.Length;
            string entityName = fullName.Substring(lastIndexOf, length - lastIndexOf);

            return entityName + "ID";
        }        
    }
}